import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
import '../../components/appbar.dart';
import '../../utils/methods.dart';
import '../../config/colors.dart';
import '../../utils/ajax.dart';
import '../../config/apis.dart';
import '../../components/toast.dart';
import '../../components/dialog.dart';
import '../../routes/routes.dart';

class ChooseSubject extends StatelessWidget {
  
  Widget title() {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            '请选择你要考试的题库',
            style: TextStyle(
              fontSize: fitSize(40) 
            ),
          ),
          SizedBox(height: fitSize(20)),
          Text(
            '请选择驾照类型',
            style: TextStyle(
              color: Colors.black87
            ),
          )
        ],
      ),
    );
  }

  // 车型列表
  Widget carsType() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(
        left: fitSize(30),
        right: fitSize(30),
        top: fitSize(20)
      ),
      margin: EdgeInsets.only(
        top: fitSize(50) 
      ),
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(color: Color(AppColors.grayColorLight))
        ) 
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('驾驶证/资格证 (当前类型：小车)', style: TextStyle(fontSize: fitSize(32))),
          Row(
            children: <Widget>[
              cardTypeItem()
            ], 
          )
        ],
      )
    );
  }

  Widget cardTypeItem() {
    return Container(
      width: fitSize(160),
      height: fitSize(160),
      margin: EdgeInsets.only(
        top: fitSize(30) 
      ),
      decoration: BoxDecoration(
        color: Color(AppColors.grayColorLight),
        border: Border.all(
          color: Color(AppColors.assistBlue) 
        )
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            'assets/pages/start/car1.png',
            width: fitSize(60),
            height: fitSize(60) 
          ),
          Text(
            '小车',
            style: TextStyle(
              fontSize: fitSize(30)
            )
          ),
          SizedBox(height: fitSize(10)),
          Text(
            'C1/C2/C3',
            style: TextStyle(color: Color(AppColors.grayColor99), fontSize: fitSize(24))
          )
        ], 
      ),
    );
  }

  void loadExamLibs(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return new LoadingDialog(
          text: "题库加载中…",
        );
      }
    );
    Map postData = { 
      "body": { 
        "moduleid": "X034", 
        "openid": "ox33W5cuD4K4j5OdOcuTFuwBD5bM", 
        "query_type": "顺序", 
        "rankType": 1, 
        "subject": 1 
      },
      "moduleid": "X034" 
    };
    ajaxPost(Apis.exercises, postData).then((result) {
      if(result['code'] == '0') {
        String subject1 = json.encode(result['data']);
        setStorage('subject1', subject1);
        Navigator.pop(context);
        Routes.router.navigateTo(
          context,
          '/navs',
          transition: TransitionType.inFromRight,
          clearStack: true
        );
      }else{
        Navigator.pop(context);
        Toast(context: context, tips: result['message']).showToast();
      }
    }).catchError((err) {
      Navigator.pop(context);
      Toast(context: context, tips: '获取科目一题库失败', icon: Icons.error).showToast();
    });
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWith3Part(
        height: fitSize(95), 
        content: Text(
          '题库设置'
        )
      ), 
      body: Container(
        width: size.width,
        padding: EdgeInsets.only(
          top: fitSize(40) 
        ),
        child: Stack(
          children: <Widget>[
            Column(
              children: <Widget>[
                title(),
                carsType(),
              ],
            ),
            Positioned(
              left: 0,
              bottom: 0,
              child: GestureDetector(
                onTap: () {
                  loadExamLibs(context);
                },
                child: Container(
                  width: size.width,
                  height: fitSize(90),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Color(AppColors.assistBlue) 
                  ),
                  child: Text('确定选择', style: TextStyle(color: Colors.white, fontSize: fitSize(34))), 
                ) 
              )
            )
          ],
        )
      ),
    );
  }
}